//go:build wireinject
// +build wireinject

package server

import (
	"context"
	"gin-init/configs"
	"gin-init/internal/api"
	"gin-init/internal/repository"
	"gin-init/internal/router"
	"gin-init/internal/scheduled_task"

	"github.com/gin-gonic/gin"
	"github.com/google/wire"
)

type Injector struct {
	Engine        *gin.Engine
	ScheduledTask *scheduled_task.ScheduledTask
}

func InitGinEngine(r router.IRouter) (*gin.Engine, error) {
	if configs.IsDebug() {
		gin.SetMode(gin.ReleaseMode)
	} else {
		gin.SetMode(gin.DebugMode)
	}

	engine := gin.Default()

	// Router register
	err := r.Register(engine)
	if err != nil {
		return nil, err
	}

	return engine, nil
}

func BuildInjector(ctx context.Context) (*Injector, error) {
	wire.Build(
		router.RouterSet,
		InitGinEngine,
		repository.RepositorySet,
		scheduled_task.ScheduledTaskSet,
		api.ApiSet,
		wire.Struct(new(Injector), "*"),
	)
	return new(Injector), nil
}
